<?php

class ModeratorsController extends AppController {

    var $name = 'Moderators';
    var $uses = 'User';
    var $helpers = array('session');
    var $components = array('RequestHandler', 'Cookie', 'Captcha');
    var $paginate = array(
        'limit' => '10'
    );

    function beforeFilter() {
        parent::beforeFilter();

        // echo $this->Session->setFlash("Welcome user");
        //$this->Auth->allow();
        $this->Auth->loginRedirect = array('controller' => 'Users', 'action' => 'index');
    }

    function admin_profile() {
        
        $data = $this->User->find('first', array('conditions' => array('User.id' => $_SESSION['Auth']['User']['id'])));
        $this->set('user', $data);
    }

    function admin_edit($id=null) {
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $_SESSION['Auth']['User']['id']);
        } else {
            //debug($this->data);die();
            if (!isset($this->data['UserProfile']['img'])){
                if($this->data['UserProfile']['image']['error'] == '0') {
            
                $ext = pathinfo(WWW_ROOT . 'img/uploads/Moderators/' . $this->data['UserProfile']['image']['name']);
                if ($this->check_image_extension($ext['extension'])) {
                    $image_name = uniqid('RTU', true) . '.' . $ext['extension'];
                    $filename = WWW_ROOT . 'img/uploads/Moderators/' . $image_name;
                    if (!file_exists(WWW_ROOT . 'img/uploads/Moderators/' . $image_name)) {
                        move_uploaded_file($this->data['UserProfile']['image']['tmp_name'], WWW_ROOT . 'img/uploads/Moderators/' . $image_name);
                    }

                    $this->data['UserProfile']['image'] = $image_name;
                }
            } 
            else{
                $this->data['UserProfile']['image'] = null;
            }}else{
                $this->data['UserProfile']['image'] = $this->data['UserProfile']['img'];
            }


            $this->User->id = $this->data['User']['id'];
            if ($this->User->saveAll($this->data)) {

                $this->Session->setFlash('Profile Updated');
                $this->redirect(array('controller' => 'Moderators', 'action' => 'admin_profile'));
            } 
        }
    }

    function check_image_extension($ext) {
        $response = false;
        $allowedext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'pjpeg', 'png', 'PNG');
        foreach ($allowedext as $extension) {
            if ($extension == $ext) {
                $response = true;
                break;
            }
        }
        return $response;
    }

    function admin_delete_image($id=null) {
        if (!empty($id)) {

            $query = mysql_query("UPDATE `user_profiles` set `image`='' WHERE id='" . $id . "'");
            $data = $this->User->UserProfile->find('first', array('conditions' => array('UserProfile.user_id =' => $id)));
            $dir = WWW_ROOT . 'img/uploads/Moderators/' . $data['UserProfile']['image'];
            $file = new File($dir);
            if ($query) {
                if ($file) {
                    $file->delete();
                    $this->redirect(array('controller' => 'Moderators', 'action' => 'edit',$id));
                }
            }
        }
    }
    function admin_change_password(){
        if($this->RequestHandler->isAjax()){
            Configure::write('debug', 0);
            $this->autoRender=false;
          
             if(isset($_POST['old_pass'])){
            $data= $this->User->find('first',array('conditions'=>array('User.id '=>$_SESSION['Auth']['User']['id'],'User.password '=>$this->Auth->password($_POST['old_pass']))));
           if($data)     {
                $query=mysql_query("UPDATE user set `password`='".$this->Auth->password($_POST['new_pass'])."'");
                return true;
            }else{
                return false;
            }
            
        }
        }
    }

}

?>
